*Main data pathway globals, set directory to clean data folder
global results "S:\Project\DemoSos2\common\felles\JR_RG\DrVA\ResultsRev\"
global cleandata "S:\Project\DemoSos2\common\felles\JR_RG\DrVA\CleanData\"
cd "$cleandata"

*Make new folder and subfolders 
capture mkdir "$cleandata"
capture mkdir "$results"

cap log close
clear all
set more off
set matsize 11000
set maxvar 120000


*Load data
use  clean_patientlevel_file_rev3.dta, clear
keep if yr_str_exog_swap >= 2005 & yr_str_exog_swap<=2014
keep if str_exogGPIDnew!=. 


* largest mobility group 
a2group, individual(str_exogGPIDnew) unit(str_exogGPIDprev) groupvar(pair)
bys pair: ge size_pair = _N
tab pair
drop if size_pair < 10 ///largest mobility group has 99%

keep if str_exog_age >=55



ge death5 = mortality_5year
ge death2 = mortality_2year
for num 25 35 45 55: ge death5_X = mortality_5year if str_exog_age>=X
for num 25 35 45 55: ge death2_X = mortality_2year if str_exog_age>=X
for var heart cancer ext resp mental accident suicide homicide: ge X2y_55 = X_2y if str_exog_age>=55

label var death2_55 	"2-years mortality for 55+"
label var bweight_2y 	"Birthweight"
label var heart2y_55 	"Mortality: heart conditions"
label var cancer2y_55 	"Mortality: cancer"
label var ext2y_55 		"Mortality: external conditions"
label var resp2y_55 	"Mortality: respiratory conditions"
label var mental2y_55 	"Mortality: mental health conditions"
label var accident2y_55	"Mortality: accident conditions"

/* estimate the FE */
global index "death2_55"


*patient
ge old = str_exog_age>= 65


*doc
ge dr_long_list = dr_list_length_str_exog_swap > 1100 if dr_list_length_str_exog_swap!=.
ge dr_old = dr_age_str_exog_swap >=40 if dr_age_str_exog_swap!=.
ge dr_max_list =  dr_max_str_exog_swap> 1200 if  dr_max_str_exog_swap!=.
ge dr_capacity = dr_max_str_exog_swap-dr_list_length_str_exog_swap >=0

rename dr_group_str_exog_swap dr_group
rename dr_share_str_exog_swap dr_share
rename dr_male_str_exog_swap dr_male 
rename dr_spec_str_exog_swap dr_spec
rename dr_long_list dr_list

local variation "male old hs0 dr_group dr_share dr_male dr_spec dr_list dr_old"

lab var male	"Male"
lab var old  	"Over 65 y.o."
lab var hs0 		"At most HS"
lab var dr_group "Group Practice"
lab var dr_share "Shared List"
lab var dr_male "Male GP"
lab var dr_spec "Specialist GP"
lab var dr_list "GP list >1100"
lab var dr_old 	"GP >40 y.o."


* figure A9
foreach x in $index {
	foreach var in `variation' {
		foreach num in 0 1 {
			
			reghdfe `x' i.yr_str_exog_swap i.str_exog_age if `var'==`num', a(_fe_`x'_`var'`num'=str_exogGPIDnew str_exogGPIDprev) res(res_`x'_`var'`num')  

			ge fe2_`x'_`var'`num' = _fe_`x'_`var'`num' + res_`x'_`var'`num' if e(sample) 
			bys str_exogGPIDnew: egen fe_`x'_`var'`num' = mean(fe2_`x'_`var'`num') 
			drop _fe_* fe2*
	
		}
	
		ge 		f_`x'_`var' = fe_`x'_`var'0 if `var'==0
		replace f_`x'_`var' = fe_`x'_`var'1 if `var'==1
		ksmirnov f_`x'_`var', by(`var') 
		local pv_`x'_`var': display %9.3f r(p) 
		
		local mylab: variable label `var'

		twoway 	(kdensity fe_`x'_`var'1, lcolor(red) lpattern(dash) lwidth(thick) graphregion(color(white)) bgcolor(white) xtitle(Value Added)) ///
				(kdensity fe_`x'_`var'0, lcolor(blue) lpattern(solid) lwidth(thick)), legend(order(1 "`mylab' 1" 2 "`mylab' 0")) caption(`pv_`x'_`var'') 
		graph export "$results\h`x'_`var'.pdf", replace 
	}
}

foreach var in $index {
	label var fe_`var'_male1 "Male Patient"
	label var fe_`var'_male0 "Female Patient"
	label var fe_`var'_old1 "Patient 65+"
	label var fe_`var'_old0 "Patient under 65"
*	label var fe_`var'_bigcity1 "5 Largest Cities"
*	label var fe_`var'_bigcity0 "Rural"
	label var fe_`var'_hs01 "High School"
	label var fe_`var'_hs00 "More than HS"

	label var fe_`var'_dr_group1 "GP in group office"
	label var fe_`var'_dr_group0 "GP not in group office"
	
	label var fe_`var'_dr_share1 "GP shares list"
	label var fe_`var'_dr_share0 "GP doesnt share list"
	
	label var fe_`var'_dr_male1 "GP is male"
	label var fe_`var'_dr_male0 "GP is female"
	
	label var fe_`var'_dr_spec1 "GP specialist"
	label var fe_`var'_dr_spec0 "GP not specialist"
	
	label var fe_`var'_dr_list1 "GP has long list"
	label var fe_`var'_dr_list0 "GP has short list"
	
	label var fe_`var'_dr_old1 "GP is 40+"
	label var fe_`var'_dr_old0 "GP is less than 40"
	
}


/* TABLE A14*/
bys str_exogGPIDnew: gen count_doc = _n
keep if count_doc==1

eststo s1: estpost tabstat fe_death2_55_*, s(n mean sd) columns(statistics)  
esttab s1 using "$results/fe_unadjusted_het.tex", replace fragment ///
	cells("count(fmt(0)) mean(fmt(3)) sd(fmt(3))") noobs nomtitle ///
	varlabels (`e(labels)')  varwidth(20) booktabs label
esttab s1 using "$results/fe_unadjusted_csv.tex", replace ///
	cells("count(fmt(0)) mean(fmt(3)) sd(fmt(3))") noobs nomtitle ///
	varlabels (`e(labels)')  varwidth(20)  label
	
keep str_exogGPIDnew fe_* 
duplicates drop
save "$results/fe_addon_het.dta", replace




	
